//package com.jianying.video.decode;
//
//import android.graphics.SurfaceTexture;
//import android.media.MediaCodec;
//import android.media.MediaFormat;
//import android.util.Log;
//import android.view.Surface;
//
//import com.jianying.video.log.LogUtil;
//
//import java.nio.ByteBuffer;
//import java.util.HashMap;
//
///**
// * Created by wanglvbo on 2019/3/6.
// */
//
//public class MediaCodecDecodeSaveVideo {
//    public static int idthis = -1;
//    public static HashMap<Long, MediaCodecDecodeSaveVideo> mapMedia = new HashMap<Long, MediaCodecDecodeSaveVideo>();
//    public MediaCodecDecodeSaveVideo(){
//        idthis++;
//        thisidadd = idthis;
//        LogUtil.w("MediaCodecDecode MediaCodecDecode new  id= "+idthis);
//    }
//    /**
//     * 准备
//     *
//     */
////    private native void enginePrepared(long avMediaReaderAdd);
//    /**
//     * 播放文件路径
//     */
//    private String dataSource;
//    /**
//     * 硬解码mime
//     */
//    private MediaFormat mediaFormat;
//    /**
//     * 视频硬解码器
//     */
//    private MediaCodec mediaCodec;
//    /**
//     * 渲染surface
//     */
//    /**
//     * 视频解码器info
//     */
//    private int thisidadd = -1;
//    private MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
//    private Surface surface;
//    private SurfaceTexture surfaceTexture;
//    private boolean isupdateTextureOk = false;
//    private int onFrameAvailableCount = 0;
//    public int mediacodecInit(long avMediaReaderId,int textureid_mediacodec,int mimetype, int width, int height, byte[] csd0, byte[] csd1)
//    {
//        if(textureid_mediacodec < 0){
//            return -1;
//        }
//        int result = -1;
//        LogUtil.w("MediaCodecDecode textureid_mediacodec "+textureid_mediacodec);
//        if(surface == null){
//            surfaceTexture = new SurfaceTexture(textureid_mediacodec);
//            surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
//                @Override
//                public void onFrameAvailable(SurfaceTexture surfaceTexture) {
//                    isupdateTextureOk = true;
//                    onFrameAvailableCount++;
//                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  onFrameAvailable  onFrameAvailable havavideoCount "+havavideoCount+"  onFrameAvailableCount "+onFrameAvailableCount);
////                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index+"  havavideoCount "+havavideoCount);
//
//                }
//            });
//            surface = new Surface(surfaceTexture);
//        }
//
//        if(surface != null)
//        {
//            try {
//                String mtype = "video/avc";//getMimeType(mimetype);
//                mediaFormat = MediaFormat.createVideoFormat(mtype, width, height);
//                mediaFormat.setInteger(MediaFormat.KEY_WIDTH, width);
//                mediaFormat.setInteger(MediaFormat.KEY_HEIGHT, height);
//                int maxPixels = ((width + 15) / 16) * ((height + 15) / 16) * 16 * 16;
////                int minCompressionRatio = 2;
//
////                int maxInputSize = (maxPixels * 3) / (2 * minCompressionRatio);
//                mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, maxPixels);
////                mediaFormat.setLong(MediaFormat.KEY_MAX_INPUT_SIZE, width * height);
//                mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 0);
//                mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(csd0));
//                mediaFormat.setByteBuffer("csd-1", ByteBuffer.wrap(csd1));
//                Log.d("ywl5320", mediaFormat.toString());
//                mediaCodec = MediaCodec.createDecoderByType(mtype);
//                if(surface != null)
//                {
//                    mediaCodec.configure(mediaFormat, surface, null, 0);
//                    mediaCodec.start();
//                }
//                result = 1;
//            } catch (Exception e) {
//                result = -1;
//                e.printStackTrace();
//            }
//
////            result = -1;
//
//            if(result != 1){
//                stop();
//            }else{
//                mapMedia.put(Long.valueOf(avMediaReaderId),this);
//            }
//        }
//
//        return result;
//    }
//    public int dataIndex = -1;
//    public int havavideoCount = 0;
//    public int pakageCount = 0;
//    boolean isfrist = true;
//    public int mediacodecDecode(byte[] bytes, int size, int pts)
//    {
//
//        if(size == 4){
//            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " pakageCount  size=4 "+pakageCount);
//        }else{
//            pakageCount++;
//            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " pakageCount   "+pakageCount);
//        }
//        int result = 0;
//        isupdateTextureOk = false;
//        boolean isWaitTextureUpdate = true;//是否需要等待
////        LogUtil.w("MediaCodecDecode mediacodecDecode "+size+"   bytes.length "+bytes.length+"  dithis = "+thisidadd);
//        if(bytes != null && mediaCodec != null && info != null)
//        {
//
//            try
//            {
//                if(size == 4){
//                    if(isfrist){
//                        isfrist = false;
//                        int inputBufferIndex = mediaCodec.dequeueInputBuffer(10000);
//
//                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "   inputBufferIndex "+inputBufferIndex);
//                        while(inputBufferIndex<0){
//                            Thread.sleep(50);
//                            inputBufferIndex = mediaCodec.dequeueInputBuffer(10000);
//                            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " sleep  inputBufferIndex "+inputBufferIndex);
//                        }
//                        if(inputBufferIndex >= 0)
//                        {
////                            ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndex];
////                            byteBuffer.clear();
////                            byteBuffer.put(bytes);
////                            mediaCodec.queueInputBuffer(inputBufferIndex, 0, 4, pts, 0);
//                            mediaCodec.queueInputBuffer(
//                                    inputBufferIndex, 0, 0, -1, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
////                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " inputBufferIndex>>>>>0  pakageCount "+pakageCount);
//                        }
//                    }
//
//                }else{
//                    int inputBufferIndex = mediaCodec.dequeueInputBuffer(10000);
//                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "   inputBufferIndex "+inputBufferIndex);
//                    while(inputBufferIndex<0){
////                        Thread.sleep(50);
//                        inputBufferIndex = mediaCodec.dequeueInputBuffer(10000);
//                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " sleep  inputBufferIndex "+inputBufferIndex);
//                    }
//                    if(inputBufferIndex >= 0)
//                    {
//                        ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndex];
//                        byteBuffer.clear();
//                        byteBuffer.put(bytes);
//                        mediaCodec.queueInputBuffer(inputBufferIndex, 0, size, pts, 0);
////                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " inputBufferIndex>>>>>0  pakageCount "+pakageCount);
//                    }
//                }
//
//                LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " dequeueOutputBuffer "+"  index = start ");
//                int index = mediaCodec.dequeueOutputBuffer(info, 10000);
//
//                LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " dequeueOutputBuffer "+"  index = "+index+"  time "+info.presentationTimeUs);
//                if(index < 0){
//                    result = 1;
//                }else{
//                    result = 0;
//                }
//
//                if(index>0){
//                    havavideoCount++;
//                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index+"  havavideoCount "+havavideoCount);
//                    isupdateTextureOk = false;
//                    mediaCodec.releaseOutputBuffer(index, true);
//                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender start ");
//                    long startTime = System.currentTimeMillis();
////                    while (!isupdateTextureOk && pakageCount >= havavideoCount) {
////                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender end while "+havavideoCount);
////                        updateRender();
////                        if(System.currentTimeMillis() - startTime>100){
////                            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender end while time "+havavideoCount);
////                            break;
////                        }
////                    }
//                    updateRender();
//                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender end ");
//                }
//            }catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//        }
//        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "receiveVideoFrameFromDecoder   isHaveVideo= "+result);
//        return result;
//    }
//
////    public int dataIndex = -1;
////    List<PackageData> dataList = new ArrayList<PackageData>();
////    public int seadDataToMediaCodec(byte[] bytes, int size, int pts){
////        if (bytes != null && mediaCodec != null && info != null) {
////            try {
////                int inputBufferIndexthisdata = mediaCodec.dequeueInputBuffer(5000);
////                if (inputBufferIndexthisdata >= 0) {
////                    ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndexthisdata];
////                    byteBuffer.clear();
////                    byteBuffer.put(bytes);
////                    mediaCodec.queueInputBuffer(inputBufferIndexthisdata, 0, size, pts, 0);
////                }
////
////                dataIndex = mediaCodec.dequeueOutputBuffer(info, 10000);
//////                LogUtil.w("MediaCodecDecode dithis" + thisidadd + " dequeueOutputBuffer " + size + "  index = " + indexthisdata);
////                if (dataIndex >= 0) {//如果有了
////
////                    return 0;
////                } else {
////
////                }
////            } catch (Exception e) {
////                e.printStackTrace();
////            }
////        }
////        return 1;
////    }
////
//    //                while (index >= 0) {
////                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index);
//////                    ByteBuffer outputBuffer = null;
//////                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//////                        outputBuffer = mediaCodec.getOutputBuffer(index);
//////                    } else {
//////                        outputBuffer = mediaCodec.getOutputBuffers()[index];
//////                    }
//////                    if (outputBuffer != null) {
//////                        outputBuffer.position(0);
//////                        outputBuffer.limit(info.offset + info.size);
//////                        byte[] yuvData = new byte[outputBuffer.remaining()];
//////                        outputBuffer.get(yuvData);
//////
//////                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222"+"  updateRender start "+yuvData.length+"   "+yuvData[40]);
//////
//////                        mediaCodec.releaseOutputBuffer(index, false);
//////                        outputBuffer.clear();
//////                    }
////
////                    mediaCodec.releaseOutputBuffer(index, true);
////                    index = mediaCodec.dequeueOutputBuffer(info, 10000);
////
////                    havavideoCount++;
////                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index+"  havavideoCount "+havavideoCount);
////                }
//
////    public int mediacodecDecode(byte[] bytes, int size, int pts)
////    {
////        isupdateTextureOk = false;
////        int result = 1;//没有数据 需要继续获取
////        boolean isHaveImage = false;
////        boolean isWaitTextureUpdate = true;//是否需要等待
////        if(dataIndex>=0){
////            PackageData obj = new PackageData();
////            obj.data = bytes;
////            obj.size = size;
////            obj.pts = pts;
////            dataList.add(0,obj);
////            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "1111  "+"  index = "+dataIndex);
////            mediaCodec.releaseOutputBuffer(dataIndex, true);
////            dataIndex = mediaCodec.dequeueOutputBuffer(info, 10000);
////            isHaveImage = true;
////        }else {
////            if (dataList.size() > 0) {
////
////                while (dataList.size() > 0) {
////                    PackageData obj = dataList.get(dataList.size() - 1);
////                    dataList.remove(obj);
////                    byte[] bytesthis = obj.data;
////                    int sizethis = obj.size;
////                    int ptsthis = obj.pts;
////                    int res = seadDataToMediaCodec(bytesthis,sizethis,ptsthis);
////
////
////                    if (bytesthis != null && mediaCodec != null && info != null) {
////                        try {
////                            int inputBufferIndex = mediaCodec.dequeueInputBuffer(5000);
////                            if (inputBufferIndex >= 0) {
////                                ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndex];
////                                byteBuffer.clear();
////                                byteBuffer.put(bytesthis);
////                                mediaCodec.queueInputBuffer(inputBufferIndex, 0, sizethis, ptsthis, 0);
////                            }
////
////                            int index = mediaCodec.dequeueOutputBuffer(info, 10000);
////                            LogUtil.w("MediaCodecDecode dithis" + thisidadd + " dequeueOutputBuffer " + size + "  index = " + index);
////                            if (index < 0) {//如果没有
////                                if (dataList.size() == 0) {
////                                    if (bytes != null && mediaCodec != null && info != null) {
////                                        try {
////                                            int inputBufferIndexthisdata = mediaCodec.dequeueInputBuffer(5000);
////                                            if (inputBufferIndex >= 0) {
////                                                ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndexthisdata];
////                                                byteBuffer.clear();
////                                                byteBuffer.put(bytes);
////                                                mediaCodec.queueInputBuffer(inputBufferIndexthisdata, 0, size, pts, 0);
////                                            }
////
////                                            int indexthisdata = mediaCodec.dequeueOutputBuffer(info, 10000);
////                                            LogUtil.w("MediaCodecDecode dithis" + thisidadd + " dequeueOutputBuffer " + size + "  index = " + index);
////                                            if (indexthisdata >= 0) {//如果有了
////                                                result = 0;
////                                                isWaitTextureUpdate = true;
////                                            } else {
////
////                                            }
////                                        } catch (Exception e) {
////                                            e.printStackTrace();
////                                        }
////                                    }
////                                    break;
////                                }
////                                continue;
////                            } else {
////                                result = 0;
////                                isWaitTextureUpdate = true;
////                                obj = new PackageData();
////                                obj.data = bytes;
////                                obj.size = size;
////                                obj.pts = pts;
////                                dataList.add(0, obj);
////                            }
////                        } catch (Exception e) {
////                            e.printStackTrace();
////                        }
////                    }
////                }
////            } else {//如果进来还一无所有
////                int res = seadDataToMediaCodec(bytes,size,pts);
////
////            }
////        }
////        updateRender();
////        return result;
////
//////            int result = 0;
//////
//////            if(bytes != null && mediaCodec != null && info != null)
//////            {
//////                try
//////                {
//////                    int inputBufferIndex = mediaCodec.dequeueInputBuffer(5000);
//////                    if(inputBufferIndex >= 0)
//////                    {
//////                        ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndex];
//////                        byteBuffer.clear();
//////                        byteBuffer.put(bytes);
//////                        mediaCodec.queueInputBuffer(inputBufferIndex, 0, size, pts, 0);
//////                    }
//////
//////                    int index = mediaCodec.dequeueOutputBuffer(info, 10000);
//////                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " dequeueOutputBuffer "+size+"  index = "+index);
//////                    if(index < 0){
//////                        result = 1;
//////                    }else{
//////                        result = 0;
//////                    }
//////                    if(index<0){//没有数据则不等
//////                        isWaitTextureUpdate = false;
//////                    }
////////                    while (index >= 0) {
////////                        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index);
////////                        mediaCodec.releaseOutputBuffer(index, true);
////////                        index = mediaCodec.dequeueOutputBuffer(info, 10000);
////////                    }
//////
//////                }catch (Exception e)
//////                {
//////                    e.printStackTrace();
//////                }
//////            }
//////            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender start ");
////////        while (isWaitTextureUpdate && !isupdateTextureOk){
////////
////////        }
//////
//////            updateRender();
//////
//////            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender end ");
//////            LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "receiveVideoFrameFromDecoder   isHaveVideo= "+result);
//////            return result;
//////        }
//////
//////        int result = 0;
//////        isupdateTextureOk = false;
//////        boolean isWaitTextureUpdate = true;//是否需要等待
////////        LogUtil.w("MediaCodecDecode mediacodecDecode "+size+"   bytes.length "+bytes.length+"  dithis = "+thisidadd);
//////        if(bytes != null && mediaCodec != null && info != null)
//////        {
//////            try
//////            {
//////                int inputBufferIndex = mediaCodec.dequeueInputBuffer(5000);
//////                if(inputBufferIndex >= 0)
//////                {
//////                    ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[inputBufferIndex];
//////                    byteBuffer.clear();
//////                    byteBuffer.put(bytes);
//////                    mediaCodec.queueInputBuffer(inputBufferIndex, 0, size, pts, 0);
//////                }
//////
//////                int index = mediaCodec.dequeueOutputBuffer(info, 10000);
//////                LogUtil.w("MediaCodecDecode dithis"+thisidadd+ " dequeueOutputBuffer "+size+"  index = "+index);
//////                if(index < 0){
//////                    result = 1;
//////                }else{
//////                    result = 0;
//////                }
//////                if(index<0){//没有数据则不等
//////                    isWaitTextureUpdate = false;
//////                }
//////                while (index >= 0) {
//////                    LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "22222222  "+"  index = "+index);
//////                    mediaCodec.releaseOutputBuffer(index, true);
//////                    index = mediaCodec.dequeueOutputBuffer(info, 10000);
//////                }
//////
////////                if (index >= 0) {
////////                    // outputBuffer is ready to be processed or rendered.
////////                    decoder.releaseOutputBuffer(outputBufferIndex, true);
////////                } else if (index == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
////////                    outputBuffers = decoder.getOutputBuffers();
////////                } else if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
////////                    // Subsequent data will conform to new format.
////////                    format = decoder.getOutputFormat();
////////                }
//////            }catch (Exception e)
//////            {
//////                e.printStackTrace();
//////            }
//////        }
//////        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender start ");
////////        while (isWaitTextureUpdate && !isupdateTextureOk){
////////
////////        }
//////
//////        updateRender();
//////
//////        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "  "+"  updateRender end ");
//////        LogUtil.w("MediaCodecDecode dithis"+thisidadd+ "receiveVideoFrameFromDecoder   isHaveVideo= "+result);
//////        return result;
////    }
//
//    public void updateRender(){//更新纹理
//        surfaceTexture.updateTexImage();
//    }
//    private String getMimeType(int type)
//    {
//        if(type == 1)
//        {
//            return "video/avc";
//        }
//        else if(type == 2)
//        {
//            return "video/hevc";
//        }
//        else if(type == 3)
//        {
//            return "video/mp4v-es";
//        }
//        else if(type == 4)
//        {
//            return "video/x-ms-wmv";
//        }
//        return "";
//    }
//    public void stop()
//    {
//        if(mediaCodec != null)
//        {
//            try
//            {
//                mediaCodec.flush();
//                mediaCodec.stop();
//                mediaCodec.release();
//            }
//            catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//            mediaCodec = null;
//            mediaFormat = null;
//        }
//        if(surface != null)
//        {
//            try
//            {
//                surface.release();
//            }
//            catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//            surface = null;
//        }
//        if(surfaceTexture != null){
//
//            try
//            {
//                surfaceTexture.release();
//            }
//            catch (Exception e)
//            {
//                e.printStackTrace();
//            }
//            surfaceTexture = null;
//        }
//    }
//}
